#!/bin/sh -f
# $Id: cvsd 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy  <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# Program name
PROG=`basename $0`

# UID value
UID=`id -u`

# Globals
RUNDIR=
PID_FILE=
CHROOT="/usr/sbin/chroot"
CHROOT_DIR="/var/www"
SSHD="/usr/sbin/sshd"
RM="/bin/rm -f"

usage () {
   printf "${PROG} options [ commands ]\n"
   printf "options: \n"
   printf " -d [ specify the chroot directory, default: /var/www ]\n"
   printf " -h [ prints this help ]\n"
   printf "commands: \n"
   printf "\t start     [ Start CVS daemon using SSH ]\n"
   printf "\t stop      [ Stop CVS daemon ]\n"
   printf "\t status    [ Status of the CVS daemon ]\n"
   printf "\t restart   [ Restart CVS daemon ]\n"
   exit 1
}

start_cvsd () {
   if [ -f ${PID_FILE} ]; then
      status_cvsd
      exit 1
   fi

   if [ -x ${CHROOT} -a -x "${CHROOT_DIR}/${SSHD}" ]; then
      ${CHROOT} ${CHROOT_DIR} ${SSHD}
      status_cvsd
   fi
}

stop_cvsd () {
   if [ -f ${PID_FILE} ]; then
      PID=`tail -1 ${PID_FILE}`
      kill -TERM ${PID}
      echo "${PROG} with pid ${PID} stopped"
      ${RM} ${PID_FILE}
   else
      printf "${PROG}: no ${PID_FILE} found, cannot stop cvsd"
      exit 1
   fi
}

status_cvsd () {
   if [ -f ${PID_FILE} ]; then
      PID=`tail -1 ${PID_FILE}`
      echo "${PROG} running, pid: ${PID}"
   fi
}

# If we do not have enough arguments, then 
if [ $# -lt 1 ]; then
   usage
else 
   if [ ${UID} -ne 0 ]; then
      printf "${PROG}: needs to have root privileges\n"
      exit 1
   fi

   while getopts d:h arg; do
      case ${arg} in
         d)
            CHROOT_DIR=${OPTARG}
            ;;

         h)
            usage
            ;;

         *)
            usage
            ;;
      esac
   done

   arg=`echo $* | awk '{ print $NF; }'`

   if [ ! -d ${CHROOT_DIR} ]; then
      printf "${PROG}: ${CHROOT_DIR} no such directory\n"
      exit 1
   fi

   RUNDIR="${CHROOT_DIR}/var/run"

   if [ ! -d ${RUNDIR} ]; then
      printf "${PROG}: /var/run directory does not exist under ${CHROOT_DIR}"
      exit 1
   fi

   PID_FILE="${RUNDIR}/sshd.pid"

   case ${arg} in
      start)
         start_cvsd
         ;;

      stop)
         stop_cvsd
         ;;

      status)
         status_cvsd
         ;;

      restart)
         stop_cvsd
         start_cvsd
         ;;

      *)
         usage
         ;;

   esac
fi
